<template>
  <div class="categray">
    <div class="header-box">
      <!--添加van-hairline--bottom类名，设置底部1px边线-->
      <header class="category-header wrap van-hairline--bottom">
        <!--添加返回icon字体图标，绑定单击事件-->
        <i class="nbicon nbfanhui" @click="goHome"></i>
        <!--搜索框-->
        <div class="header-search">
          <!--模拟搜索框，单击之后跳转至商品搜索页面，在后续的章节中会讲到-->
          <i class="nbicon nbSearch"></i>
          <router-link class="search-title" to="./product-list?from=category">全场50元起步</router-link>
        </div>
        <i class="iconfont icon-More"></i>
      </header>
    </div>
    <!--主体代码div标签-->
    <div class="search-wrap" ref="searchWrap">
      <!--左边栏是大类目， 通过list-scroll组件包裹-->
      <list-scroll class="nav-side-wrapper">
        <ul class="nav-side">
          <!--循环输出结构返回 categoryData参数，设置currentIndex参数，当用户单击当前标签时，给予active类名，设置文字高亮-->
          <li
          v-for="item in categoryData"
          :key="item.categoryId"
          v-text="item.categoryName"
          :class="{'active' : currentIndex == item.categoryId}"
          @click="selectMenu(item.categoryId)"
          ></li>
        </ul>
      </list-scroll>
      <!--大类目对应的内容部分-->
      <div class="search-content">
        <!--同样采用list-scroll组件包裹-->
        <list-scroll>
          <div class="swiper-container">
            <div class="swiper-wrapper">
              <!--循环输出categoryData数组内容-->
              <template v-for="(category, index) in categoryData">
                <!--循环输出时，已经将所有类目都显示出来了，通过currentIndex参数显示当前选中的类名信息-->
                <div class="swiper-slide" v-if="currentIndex == category.categoryId" :key="index">
                  <!--在categoryData数组项内，secondLevelCategoryVOS是二级数组，代表每个二级类目-->
                  <div class="category-list" v-for="(products, index) in category.secondLevelCategoryVOS" :key="index">
                    <p class="category-title">{{ products.categoryName }}</p>
                    <!--sencondLevelCategoryVOS二级数组内有thirdLevelCategoryVOS三级数组，代表每个二级类目的单项-->
                    <div class="product-item" v-for="(product, index) in products.thirdLevelCategoryVOS" :key="index" @click="selectProduct (product)">
                      <img src="//s.weituibao.com/1583591077131/%E5%88%86%E7%B1%BB.png" class="product-img"/>
                      <p v-text="product.categoryName" class="product-title"></p>
                    </div>
                  </div>
                </div>
              </template>
            </div>
          </div>
        </list-scroll>
      </div>
    </div>
    <nav-bar></nav-bar>
  </div>
</template>
<script>
import { ref, reactive, onMounted, toRefs } from 'vue'
import NavBar from 'components/NavBar.vue' //引入公用底部导航组件
import ListScroll from 'components/ListScroll.vue' //引入滚动组件
import { useRouter } from 'vue-router' //引入路由插件
import { Toast } from 'vant' //引入提示组件
import { getCategory } from 'service/good' //引入类目服务接口

export default {
  name: 'Category',
  //注册组件
  components: {
    NavBar,
    ListScroll,
  },
  setup() {
    //生成路由实例，也可以引入入src/router/index.js中的路由实例，效果是一样的
    const router = useRouter()
    const searchWrap = ref(null)
    const state = reactive({
      categoryData: [], //类目数组
      currentIndex: 15 //默认选中第一个类目ID
    })
    onMounted(() => {
      init() //初始化方法
    })
    const init = async() => {
      //获取视图中可视区域高度
      let $screenHeight = document.documentElement.clientHeight
      //将searchWrap标签的高度减去头部和底部的高度，也就是100px
      searchWrap.value.style.height = $screenHeight - 100 + 'px'
      //显示加载中的动画
      Toast.loading('加载中...')
      //获取类目数据，结构如下
      /**
       * {
       *  categoryId: 当前类目ID
       *  categoryLevel: 当前类目级别，1表示1级，依次类推
       *  categoryName: 当前类目名称
       *  //二级类目
       *  secondLevvelCategoryVOS:[{
       *   categoryId: 当前类目ID
       *   categoryLevel: 当前类目级别，1表示1级，以此类推
       *   categoryName: 当前类目名称
       *   parentId: 当前类目的父级类目ID
       *   //三级类目
       *   thirdLevelCategoryVOS:[{
       *    categoryId:当前类目ID
       *    categoryLevel:当前类目级别，1表示1级，以此类推
       *    categoryName: 当前类目名称
       *   }]
       *  }]
       * }
       */
      const { data } = await getCategory()
      Toast.clear() //清除加载状态
      state.categoryData = data //赋值给类目变量，渲染视图
    }
    
    //返回首页的方法
    const goHome = () => {
      router.push({ path: 'home' })
    }

    //单击左侧类目，切换当前高亮，并显示当前类目信息
    const selectMenu = (index) => {
      state.currentIndex = index
    }
    //单击三级类目，跳转到商品搜索页面，直接搜索类目ID下的商品列表
    const selectProduct = (item) => {
      router.push({path: '/product-list', query: {categoryId: item.categoryId}})
    }
    //返回给template使用
    return {
      ...toRefs(state),
      goHome,
      selectMenu,
      selectProduct,
      searchWrap
    }
  }
}
</script>
<style lang="less" scoped>
@import '../theme/custom';
.categray{
  .search-wrap{
    .fj();
    width: 100%;
    margin-top: 50px;
    background: #F8F8F8;
    .nav-side-wrapper{
      width: 28%;
      height: 100%;
      overflow: hidden;
      .nav-side{
        width: 100%;
        .boxSizing();
        background: #F8F8F8;
        li{
          width: 100%;
          height: 56px;
          text-align: center;
          line-height: 56px;
          font-size: 14px;
          &.active {
            color: @primary;
            background: #fff;
          }
        }
      }
    }
  }
  .search-content{
    width: 72%;
    height: 100%;
    padding: 0 10px;
    background: #fff;
    overflow-y: scroll;
    touch-action: pan-y;
    * {
      touch-action: pan-y;
    }
    .boxSizing();
    .swiper-container{
      width: 100%;
      .swiper-slide{
        width: 100%;
        .category-main-img{
          width: 100%;
        }
        .category-list{
          display: flex;
          flex-wrap: wrap;
          flex-shrink: 0;
          width: 100%;
          .category-title{
            width: 100%;
            font-size: 17px;
            font-weight: 500;
            padding: 20px 0;
          }
          .product-item{
            width: 33.3333%;
            margin-bottom: 10px;
            text-align: center;
            font-size: 15px;
            .product-img{
              .wh(30px, 30px);
            }
          }
        }
      }
    }
  }
  .category-header{
    background: #fff;
    position: fixed;
    left: 0;
    top: 0;
    .fj();
    .wh(100%, 50px);
    line-height: 50px;
    padding: 0 15px;
    box-sizing: border-box;
    font-size: 15px;
    color: #656771;
    z-index: 10000;
    &.active{
      background: @primary;
    }
    .icon-left{
      font-size: 25px;
      font-weight: bold;
    }
    .header-search{
      display: flex;
      width: 80%;
      line-height: 20px;
      margin: 10px 0;
      padding: 5px 0;
      color: #232326;
      background: #F7F7F7;
      border-radius: 20px;
      .nbSearch{
        padding: 0 10px 0 20px;
        font-size: 17px;
      }
      .search-title{
        font-size: 12px;
        color: #666;
        line-height: 21px;
      }
    }
    .icon-More{
      font-size: 20px;
    }
  }
}
</style>